﻿/*
 Copyright (c) 2009 Alvin Cho (alvincho@risklib.com)
 Copyright (c) 2009 RiskLib.com
 
 This file is part of RiskLib.NET Project. See http://www.risklib.net for more information.
 
 RiskLib.NET is an open source project. Using of RiskLib.NET is free of charge.
 It is distributed under GNU General Public License version 2 (GPLv2).
 GPLv2 can be found at http://www.risklib.net/risklib/license.
 
 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Configuration;
using System.Text;
using System.Windows.Forms;
using RiskLib;
using RiskLib.DataSource;

namespace DataSourceExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            cbDataSource.Items.Add("Yahoo!Finance");
            cbDataSource.Items.Add("Wall Street Journal NYSE");
            cbDataSource.Items.Add("Bloomberg");
            cbDataSource.Items.Add("RiskLibSample");
            cbDataSource.SelectedIndex = 0;
            cbDataDestination.Items.Add("RiskLibSample");
            cbDataDestination.SelectedIndex = 0;
            connectionString = ConfigurationManager.ConnectionStrings["DataSourceExample.Properties.Settings.RiskLibSampleConnectionString"].ConnectionString;
            riskLibSample = new DBRiskLibSample(connectionString);
            historicalTableAdapter1.Connection = new System.Data.SqlServerCe.SqlCeConnection(connectionString);
            historicalTableAdapter1.Fill((RiskLibSampleDataSet.HistoricalDataTable)riskLibSampleDataSet.Tables["Historical"]);
        }
        bool isSaveData = false;
        string connectionString;
        AbstractDataSource dataDestination;
        DBRiskLibSample riskLibSample;
        bool isHDest = false;
        bool isFDest = false;
        private void DisplayStatus(string msg)
        {
            lbResult.Items.Insert(0, DateTime.Now.ToShortTimeString()+" "+ msg);
            Application.DoEvents();
        }
        private void btnGet_Click(object sender, EventArgs e)
        {
            isSaveData = false;
            GetData();
        }
        private void GetData()
        {

            AbstractDataSource dataSource;
            switch (cbDataSource.Text)
            {
                case "Wall Street Journal NYSE":
                    dataSource = new WSJNYSEStockDataSource();
                    break;
                case "Bloomberg":
                    dataSource = new BloombergDataSource("localhost",8194);
                    break;
                case "Yahoo!Finance":
                    dataSource = new YahooFinanceDataSource();
                    break;
                case "RiskLibSample":
                    dataSource = new DBRiskLibSample(connectionString);
                    break;
                default:
                    dataSource = new WSJNYSEStockDataSource();
                    break;
            }
            dataSource.StatusEvent += new AbstractDataSource.StatusEventHandler(dataSource_StatusEvent);
            DisplayStatus("Testing IDailyClosingSource...");
            try
            {
                IDailyClosingSource dailySource = (IDailyClosingSource)dataSource;
                dailySource.HistoricalDataEvent += new HistoricalDataEventHandler(dailySource_HistoricalDataEvent);
                dailySource.GetDailyClosing(DateTime.Today);
            }
            catch (Exception ex)
            {
                DisplayStatus("IDailyClosingSource Error: " + ex.Message);
            }
            SimpleProduct product=new SimpleProduct("IBM");
            
            DisplayStatus("Testing IHistoricalSource...");
            try
            {
                IHistoricalSource histSource = (IHistoricalSource)dataSource;
                histSource.HistoricalDataEvent += new HistoricalDataEventHandler(histSource_HistoricalDataEvent);
                histSource.GetHistoricalData(product, DateTime.Today.AddYears(-1), DateTime.Today);
            }
            catch (Exception ex)
            {
                DisplayStatus("IHistoricalSource Error: " + ex.Message);
            }
        }

        void histSource_HistoricalDataEvent(object sender, HistoricalDataEventArgs e)
        {
            DisplayStatus("HistoricalSource: " + e.Data.ToString());

            if (isSaveData && isHDest)
            {
                IHistoricalDestination histDest = (IHistoricalDestination)dataDestination;
                histDest.SaveHistoricalData(e.product, e.Data);
            }
        }

        void dataSource_StatusEvent(object sender, DataSourceEventArgs e)
        {
            DisplayStatus(e.Message);
        }

        void dailySource_HistoricalDataEvent(object sender, HistoricalDataEventArgs e)
        {
            DisplayStatus("DailyClosingSource: "+e.Data.ToString());
        }

        void InitDataDestination()
        {
            switch (cbDataDestination.Text)
            {
                case "RiskLibSample":
                    dataDestination = riskLibSample;
                    isFDest = true;
                    isHDest = true;
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            InitDataDestination();
            isSaveData = true;
            GetData();

        }

        private void riskLibSampleDataSetBindingSource_CurrentChanged(object sender, EventArgs e)
        {

        }

        private void tabControl1_TabIndexChanged(object sender, EventArgs e)
        {
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 1)
                historicalTableAdapter1.Fill((RiskLibSampleDataSet.HistoricalDataTable)riskLibSampleDataSet.Tables["Historical"]);

        }

    }
}
